export default {
	data() {
		return {
			current: 0,
			tabs: [{
				name: '待选择',
				id: 0
			}, {
				name: '待发货',
				id: 1
			}, {
				name: '已发货',
				id: 2
			}, {
				name: '已回收',
				id: ''
			}, {
				name: '已完成',
				id: 3
			}],
			limit: 10,
			list: [],
			allChoose: false,
			orders: [],
			allPrice: 0, //消费金额
			activeGoodsList: [],
			cardNum: 0,
			backNum: 0,
			status: ''
		};
	},
	onLoad(options) {
		this.current = options.type ? Number(options.type) : 0
	},
	onShow() {

	},
	async onReady() {
		uni.$on('uploadStore', res => {
			this.$refs.paging.reload()
		})
	},
	methods: {
		changeTab(index) {
			this.current = index
			this.status = this.tabs[index].id
			this.$refs.paging.reload()
		},
		async getList(pageNo, pageSize) {

			var url = ''
			if (this.current == 0) {
				url = 'server/index/getcangkulist'
			} else if (this.current == 3) {
				url = 'server/index/gethuishoulist'
			} else {
				url = 'server/index/gettihuogoods'
			}
			var res = await this.$get(url, {
				page: pageNo,
				limit: pageSize,
				goods_id: '',
				status: this.status //1 代发货  2已发货  3已完成
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				this.$refs.paging.complete(false);
				return;
			}
			this.clear()
			for (var i = 0; i < res.list.length; i++) {
				res.list[i].active = false
			}
			this.allChoose = false

			this.$refs.paging.complete(res.list);
		},
		onChoose(item) {
			item.active = !item.active

			if (this.list.every(ele => ele.active == true)) {
				this.allChoose = true
			} else {
				this.allChoose = false
			}
			this.getOrders()
		},
		onAllChoose() {
			this.allChoose = !this.allChoose
			for (var i = 0; i < this.list.length; i++) {
				this.list[i].active = this.allChoose
			}
			this.getOrders()
		},
		clear() {
			this.orders = []
			this.activeGoodsList = []
			this.allPrice = 0
			this.backNum = 0
		},
		// 计算价格，订单
		getOrders() {
			this.clear()
			for (var i = 0; i < this.list.length; i++) {
				var item = this.list[i]
				if (item.active) {
					var obj = {}
					obj.id = item.id
					obj.num = item.num

					this.orders.push(obj)
					this.activeGoodsList.push(item)
					this.allPrice += Number(Number(item.huishouprice) * Number(item.num))
					this.backNum += Number(item.num)
				}
			}
		},
		// 确认回收
		async onRecovery() {
			if (this.orders.length == 0) return this.$toast('请选择要回收的商品')
			var res = await this.$post('server/index/huishou', {
				huishoulist: JSON.stringify(this.orders),
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('回收成功')
			setTimeout(() => {
				this.$refs.paging.reload()
			}, 500)
		},
		// 发货
		onSend() {
			if (this.list.length == 0) return this.$toast('暂无可发货商品')
			if (this.orders.length == 0) return this.$toast('请选择要发货的商品')
			this.$navTo('pages/store/submit/submit', {
				goodsList: encodeURIComponent(JSON.stringify(this.activeGoodsList)),
				orders: encodeURIComponent(JSON.stringify(this.orders)),
			})
		},
		// 数量操作
		onNumChange(type, item) {
			if (type == 'jian') {
				if (item.num <= 1) {
					return
				}
				item.num--;
			} else {
				item.num++;
			}
			this.getOrders()
		},
		// 确认收货
		onShouhuo(item) {
			this.$showModal({
				title: '提示',
				content: '是否确认收货？',
				success: (res) => {
					if (res.confirm) {
						console.log('用户点击确定')
						this.shouhuoApi(item)
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		async shouhuoApi(item) {
			var res = await this.$get('server/index/mangheshouhuo', {
				id: item.id,
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$toast('确认收货成功')
			setTimeout(() => {
				this.$refs.paging.reload()
			}, 500)
		},
		// 去评价
		onComment(item) {
			this.$navTo('pages/store/comment/comment', {
				list: JSON.stringify(item.list),
				orderId: item.id
			})
		}
	}
};
